package study.nababy.recyclerview.first.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import study.nababy.recyclerview.R;

/**
 * Created by a on 2018/9/14.
 */

public class ListRecycleAdapter extends CommonAdapter<RecyclerView.ViewHolder> {

//    protected Context mContext;
//    protected int mLayoutId;
//    protected int mNum;

    // 临时数据
    private static final int[] DRAW_IDS = new int[] {
            R.drawable.tui, R.drawable.tui, R.drawable.tui,
            R.drawable.tui, R.drawable.tui
    };

    public ListRecycleAdapter(Context context, int layoutId, int num) {
        super(context, layoutId, num);
    }

    public void setNum(int num) {
        mNum = num;
    }

    public int getNum() {
        return mNum;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = View.inflate(mContext, mLayoutId, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
//        super.onBindViewHolder(viewHolder, i);
        viewHolder.mTitle.setText("I'm a " + i + " item");
        Glide.with(mContext)
                .load(DRAW_IDS[i%5])
                .fitCenter()
                .into(viewHolder.mLogo);
        // 用这种方式会卡
//        viewHolder.mLogo.setImageResource(DRAW_IDS[i%5]);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mLogo;
        private TextView mTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            mLogo = (ImageView) itemView.findViewById(R.id.logo);
            mTitle = (TextView) itemView.findViewById(R.id.texts);
        }
    }
}
